import Axios from 'axios'

const state = {
	treeData: [],		//树形结构数据
	tableData: [],		//表格数据
	menuPermission: [],		//菜单权限
	multipleSelection: [],		//选中的表格项
	selectRowId: [],		//选中的表格项id
	tableRow: [],			//当前操作的表格行数据
	tableRowId: null,		//当前表格行修改的id
	menuStatus: [],			//树形顶部菜单触发状态 (1 == 保存) (2 == 清空) (3 == 全选) (4 == 合拢展开)
}

const mutations = {
	updateTreeData(state, arr) {			//更新树形结构数据
		if(Object.prototype.toString.call(arr) !== '[object Array]') return;
		let treeData = state.treeData;
		treeData.length = 0;
		if(arr.length > 0) {
			arr.forEach((item, index) => {
				//不显示账套管理节点在页面上
				if(item['name'] === '账套管理') {
					arr.splice(index, 1);
				}
				//不能操作基础资料下面的(用户组管理)和(审批流程)节点
				if(item['id'] == 6) {
					item['submenuList'].forEach(menuItem => {
						if(menuItem['id'] == 20 || menuItem['id'] == 21) {
							menuItem['disabled'] = true;
						}
					});
				}
			});
			treeData.splice(0, 1, ...arr)
		} else {
			treeData.splice(0);
		}
	},
	updateTableData(state, arr) {			//更新表格数据
		if(Object.prototype.toString.call(arr) !== '[object Array]') return;
		let tableData = state.tableData;
		tableData.length = 0;
		arr.length > 0 ? tableData.splice(0, 1, ...arr) : tableData.splice(0);
	},
	updateTableRow(state, obj) {
		state.tableRow.splice(0, 1, obj);
	},
	updateTableRowId(state, id) {
    	state.tableRowId = id ? id : null;
    },
	updateMenuPermission(state, arr) {		//更新选中的节点数据
		if(Object.prototype.toString.call(arr) !== '[object Array]') return;
		let menuPermission = state.menuPermission,
			newArr = [];
		menuPermission.length = 0;
		arr.forEach(id => checked(id, state.treeData, newArr));
		function checked(id, arr, newArr) {
			arr.forEach(item => {
                if(item.id == id) {
                    if(item.submenuList.length == 0) {
                        newArr.push(item.id);
                    }
                } else {
                    if(item.submenuList.length != 0) {
                        checked(id, item.submenuList, newArr);
                    }
                }
            });
		}
		newArr.length > 0 ? menuPermission.splice(0, 1, ...newArr) : menuPermission.splice(0);
	},
	clearMenuPermission(state) {		//清空菜单权限
		let menuPermission = state.menuPermission;
		menuPermission.length = 0;
		if(state.tableRow[0]['name'] === '系统管理员') {
			//如果是系统管理员的话，不能清空掉用户组管理和审批流程节点
			menuPermission.splice(0, 1, ...[20, 21]);
		} else {
			menuPermission.splice(0, 1, []);
		}
	},
	updateMenuStatus(state, number) {
		state.menuStatus.splice(0, 1, number);
	},
	multipleSelection(state, arr) {			//表格选中数据触发该函数
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    }
}

const actions = {
	getMenuPermissionByTableRowId({commit}, id) {			//通过表格id获取对应的菜单权限
		Axios.get('menu/selectObjectById?id=' + id).then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					commit('updateMenuPermission', res.data.data.mids);
				}
			}
		});
	},
	getTableData({commit, rootState}) {			//获取表格数据
		Axios.get('userGroup/selectObject?systemId=' + rootState.systemId).then(res => {
			if(res.data.code == 20001) {
				if(res.data.data) {
					commit('updateTableData', res.data.data);
				}
			}
		});
	},
	getTreeDataAndGetTableData({state, commit, dispatch, rootState}) {			//同求请求多个接口（树形结构数据和表格数据）
		Axios.all([Axios.get('menu/selectObject'), Axios.get('userGroup/selectObject?systemId=' + rootState.systemId)]).then(Axios.spread((acct, perms) => {
			if(acct.data.code == 20001 && perms.data.code == 20001) {
				if(acct.data.data && perms.data.data) {
					dispatch('getMenuPermissionByTableRowId', perms.data.data[0]['id']);
					commit('updateTableRow', perms.data.data[0]);
					commit('updateTreeData', acct.data.data);
					commit('updateTableData', perms.data.data);
				}
			}
		}));
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}